<?php 
	/**
	* Auto-généré classe Evenement
	*/

	class Evenement extends \Core\ORM\Modele {
		/**
		* @var string $donnee_evenement
		*/
		protected $donnee_evenement;
		/**
		* @var \Core\ORM\TypeEvenement $typeEvenement
		*/
		protected $typeEvenement;
		/**
		* @var \Core\ORM\Criticite $criticite
		*/
		protected $criticite;
		/**
		* @var \Core\ORM\Machine $machine
		*/
		protected $machine;
		/**
		* @var \DateTime $date_evenement
		*/
		protected $date_evenement;

		/**
		* Constructeur complet
		*
		* @var int $id
		* @var string $donnee_evenement
		* @var \Core\ORM\TypeEvenement $typeevenement
		* @var \Core\ORM\Criticite $criticite
		* @var \Core\ORM\Machine $machine
		* @var \DateTime $date_evenement
		*
		* @return void
		*/
		public function __construct($id, $donnee_evenement, $typeEvenement, $criticite, $machine, $date_evenement) {
			parent::__construct($id);
			$this->setDonneeEvenement($donnee_evenement);
			$this->setTypeEvenement($typeEvenement);
			$this->setCriticite($criticite);
			$this->setMachine($machine);
			$this->setDateEvenement($date_evenement);
		}
        
        /**
        * Permet l'ajout d'un évenement
        * 
        * @return \Evenement
        */
        public function ajout() {
            if($this->criticite instanceof \Criticite && $this->criticite->getId() === 5) {
                $message = "L'évenement ".$this->typeEvenement." pour la machine ".$this->machine." rencontre un problème";
                mail("bill.gates@g-4.fr", "Alerte", $message);
            }
            parent::ajout();
        }

		//GETTERS ET SETTERS
		/**
		* Getter de l'attribut donnee_evenement
		*
		* @return string
		*/
		public function getDonneeEvenement() {
			return $this->donnee_evenement;
		}
		/**
		* Setter de l'attribut donnee_evenement
		*
		* @param string $donnee_evenement		*
		* @return void
		*/
		public function setDonneeEvenement($donnee_evenement) {
			$this->donnee_evenement=$donnee_evenement;
		}
		/**
		* Getter de l'attribut typeevenement
		*
		* @return \Core\ORM\TypeEvenement
		*/
		public function getTypeEvenement() {
			return $this->typeEvenement;
		}
		/**
		* Setter de l'attribut typeevenement
		*
		* @param \Core\ORM\TypeEvenement $typeEvenement		*
		* @return void
		*/
		public function setTypeEvenement($typeEvenement) {
			$this->typeEvenement=$typeEvenement;
		}
		/**
		* Getter de l'attribut criticite
		*
		* @return \Core\ORM\Criticite
		*/
		public function getCriticite() {
			return $this->criticite;
		}
		/**
		* Setter de l'attribut criticite
		*
		* @param \Core\ORM\Criticite $criticite		*
		* @return void
		*/
		public function setCriticite($criticite) {
			$this->criticite=$criticite;
		}
		/**
		* Getter de l'attribut machine
		*
		* @return \Core\ORM\Machine
		*/
		public function getMachine() {
			return $this->machine;
		}
		/**
		* Setter de l'attribut machine
		*
		* @param \Core\ORM\Machine $machine		*
		* @return void
		*/
		public function setMachine($machine) {
			$this->machine=$machine;
		}
		/**
		* Getter de l'attribut date_evenement
		*
		* @return \DateTime
		*/
		public function getDateEvenement($format="Y-m-d H:i:s") {
			return \Utils\Outils::getterDate($this->date_evenement, $format);
		}
		/**
		* Setter de l'attribut date_evenement
		*
		* @return void
		*/
		public function setDateEvenement($date_evenement) {
			$this->date_evenement=\Utils\Outils::setterDate($date_evenement);
		}
		public static function getAttributs() {
			return array(
							new Core\ORM\Attributs\String("donnee_evenement", false, 100, false),
							new Core\ORM\Attributs\Objet("TypeEvenement", "type_evenement_id", false, "11"),
							new Core\ORM\Attributs\Objet("Criticite", "criticite_id", false, "11"),
							new Core\ORM\Attributs\Objet("Machine", "machine_id", false, "11"),
							new Core\ORM\Attributs\DateTime("date_evenement", false, "Y-m-d H:i:s", "datetime")
						);
		}

		public static function getTable() {
			return "evenement";
		}
		
		public static function getNombresCriticites(){
			$requete = new \Core\ORM\Requete\Select();
			$sql = 'SELECT COUNT(*) as nbC, ev.criticite_id
					FROM evenement ev WHERE ev.id IN (SELECT i_ev.id
													  FROM evenement i_ev
													  WHERE i_ev.id =(SELECT i_i_ev.id
																	  FROM evenement i_i_ev
																	  WHERE i_i_ev.machine_id = i_ev.machine_id
																	  AND i_i_ev.type_evenement_id = i_ev.type_evenement_id
																	  ORDER BY i_i_ev.date_evenement DESC LIMIT 1))
					GROUP BY ev.criticite_id';
			$requete->setSql($sql);
			return $requete->execution();
		}
		
		public static function getInformation(){
			 $requete = new \Core\ORM\Requete\Select();
			 $sql = 'SELECT m.id as idM, m.nom_machine as machine, te.nom_type_evenement as service, c.nom_criticite as status, e.date_evenement as checkD, e.donnee_evenement as infos
					FROM evenement e, machine m, type_evenement te, criticite c
					WHERE e.machine_id = m.id
					AND e.type_evenement_id = te.id
					AND e.criticite_id = c.id
					AND e.id IN (SELECT i_ev.id
								  FROM evenement i_ev
								  WHERE i_ev.id = (SELECT i_i_ev.id
												   FROM evenement i_i_ev
												   WHERE i_i_ev.machine_id = i_ev.machine_id
												   AND i_i_ev.type_evenement_id = i_ev.type_evenement_id
												   ORDER BY i_i_ev.date_evenement DESC LIMIT 1))
					GROUP BY m.nom_machine, te.nom_type_evenement
					ORDER BY m.nom_machine';	

			 $requete->setSql($sql);
			 return $requete->execution();				
		}
		
		public static function getInformationByMachine($machine){
			 $requete = new \Core\ORM\Requete\Select();
			 $sql = 'SELECT m.nom_machine as machine, te.nom_type_evenement as service, c.nom_criticite as status, e.date_evenement as checkD, e.donnee_evenement as infos
					FROM evenement e, machine m, type_evenement te, criticite c
					WHERE e.machine_id = m.id
					AND e.type_evenement_id = te.id
					AND e.criticite_id = c.id
					AND e.id IN (SELECT i_ev.id
								  FROM evenement i_ev
								  WHERE i_ev.id = (SELECT i_i_ev.id
												   FROM evenement i_i_ev
												   WHERE i_i_ev.machine_id = i_ev.machine_id
												   AND i_i_ev.type_evenement_id = i_ev.type_evenement_id
												   ORDER BY i_i_ev.date_evenement DESC LIMIT 1))
					AND m.id = '.$machine->getId().'
					GROUP BY m.nom_machine, te.nom_type_evenement
					ORDER BY m.nom_machine';	

			 $requete->setSql($sql);
			 return $requete->execution();				
		}
		
        /**
        * Récupère les évènements de la Criticite passer en paramètre
        *
        * @param \Criticite $criticite 
        * 
        * @return \Evenement[]
        */
        public static function getInformations($criticite) {
            $where = "evenement.id IN   (SELECT 
                                            i_ev.id
                                        FROM evenement i_ev
                                        WHERE i_ev.id = (SELECT 
                                                            i_i_ev.id
                                                        FROM evenement i_i_ev
                                                        WHERE 
                                                            i_i_ev.machine_id = i_ev.machine_id
                                                        AND i_i_ev.type_evenement_id = i_ev.type_evenement_id
                                                        ORDER BY i_i_ev.date_evenement DESC LIMIT 1))
                    AND evenement.criticite_id = ". \Core\ORM\ORMClasse::$lien->quote($criticite->getId())."
					GROUP BY evenement_machine.nom_machine, evenement_type_evenement.nom_type_evenement
					ORDER BY evenement_machine.nom_machine";
            return Evenement::recherche($where);
        }
        
		public static function getStatusMachineUpDown(){
			$requete = new \Core\ORM\Requete\Select();	
			$sql = 'SELECT COUNT(*) as nbC, ev.criticite_id
					FROM evenement ev WHERE ev.id IN (SELECT i_ev.id
													  FROM evenement i_ev
													  WHERE i_ev.id =(SELECT i_i_ev.id
																	  FROM evenement i_i_ev
																	  WHERE i_i_ev.machine_id = i_ev.machine_id
																	  AND i_i_ev.type_evenement_id = i_ev.type_evenement_id
																	  ORDER BY i_i_ev.date_evenement DESC LIMIT 1))
                    AND type_evenement_id = 1
					AND (criticite_id = 1 OR criticite_id = 5)
					GROUP BY ev.criticite_id';
					
			$requete->setSql($sql);
			return $requete->execution();	
		}
		
		public static function getStatusEnAttente(){
			$requete = new \Core\ORM\Requete\Select();	
			$sql = 'SELECT COUNT(*) as nbC FROM machine WHERE id NOT IN (SELECT machine_id
																  FROM evenement
																  WHERE type_evenement_id = 1)';
					
			$requete->setSql($sql);
			return $requete->execution();	
		}
		
	}
?>